function IncLetter()
{
	thisIncLetter = this;
	this.TAG = " < IncLetter > ";
	
	this.letterList = null;
	this.letterTotalList = null;
	
	this.pageControl = null;
	
	this.cloneListViewItem = null;
	
	this.HTMLDom = null;
}

IncLetter.prototype = {
	getMyRecMessageList: function()
	{
		console.log(this.TAG + "getMyRecMessageList()");
		
		this.letterList = null;
		this.letterList = new Array();
		
		var mj = new MJson();
		var cm = new ConnectionManager();
		
		mj.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
		
		mj.putPub(MJson.PUB.SID, "US001");
		mj.putPub(MJson.PUB.SERVICENAME, "getMyRecMessageList");
		mj.putPub(MJson.PUB.LISTCOUNT, this.pageControl.listCount);
		mj.putPub(MJson.PUB.CURRPAGENO, this.pageControl.currentPage);

		cm.requestGet(mj.getJsonString(), function(jsonData) {
			//console.log(JSON.stringify(jsonData));
			if (mj.checkServiceResult(jsonData)) {
				if(jsonData.msp.contents)
				{
					if( jsonData.msp.contents.items ) 
					{
						if(jsonData.msp.contents.items.length) {
							for(var i=0; i<jsonData.msp.contents.items.length;i++) 
							{
								thisIncLetter.letterList.push(jsonData.msp.contents.items[i]);
							}
						}
						else 
						{
							thisIncLetter.letterList.push(jsonData.msp.contents.items);
						}
						thisIncLetter.viewList(jsonData.msp.contents["@totalcount"]);
					}
				}
			}
		});
		
	},
	onPageShow:function() 
	{
		console.log("onPageShow :: letter");
		
		this.letterTotalList = new Array();
		this.pageControl = new PageControl();
		this.pageControl.setListCount(3);
		this.pageControl.clear();
		
		var $headerMenu=$('#letter_header_menu');
		var $goInfoBooking = $headerMenu.find('a:eq(0)');
		var $goInform = $headerMenu.find('a:eq(1)');

		$($goInfoBooking).bind('click', function(event) {
		  console.log('click :: go info_booking.html');
		  $.mobile.changePage("info_booking.html");
		});
		
		$($goInform).bind('click', function(event) {
		  console.log('click :: go inform.html');
		  $.mobile.changePage("inform.html");
		});
		
		this.HTMLDom = {
            "listview" : $("#letter_listview"), // 신청자 리스트뷰
            "item" : $("#letter_listview").find('li:eq(1)').clone(true),
        };
		
		var listview = $("section#inc_letter>section").find("ul:eq(1)");
		this.cloneListViewItem = listview.find("li:eq(0)").clone(true);
		listview.empty();
		var addBtn = $("#letter_add_btn").find("button");
		addBtn.hide();
		
		this.getMyRecMessageList();
		
		
		$(".count_01").html(document.globalObj.NOTIFICATION_COUNT_DATA.booking);
		$(".count_02").html(document.globalObj.NOTIFICATION_COUNT_DATA.notification);
		$(".count_03").html(document.globalObj.NOTIFICATION_COUNT_DATA.letter);
		
	},
	viewList: function($totalCount) 
	{
		console.log(this.TAG + "viewList()");

		for(var i=0; i<thisIncLetter.letterList.length; i++) {
			thisIncLetter.letterTotalList.push(thisIncLetter.letterList[i]);
		}
		
		var listview = this.HTMLDom.listview;
		listview.empty();
		var item = this.HTMLDom.item;

		var list = thisIncLetter.letterTotalList;
		
		var util = new Util();
		
		var notReadCount = 0;
		
		for(var i=0;i<list.length; i++)
		{
			var data = list[i];
			//var senderUserInfo = data.senderUserInfo;
			
			var temp = item.clone(true);
			//방문 기록이 있을때 <li class='visit'>
			listview.append(temp);
			//data.isRead = "Y";
			
			if(data.isRead == "Y") temp.attr("class", "visit ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c");
			
			temp.find("input, label").remove();
			temp.find("a").before("<label><input type='checkbox' data-mini='true' id=letter_"+i+" /></label>");
			listview.find("input").eq(i).checkboxradio();
			temp.attr("id", i);
			temp.find("a").attr("href", "#").attr("id", i);
			var senderType = data.messageOut.senderType == "P"?"mem ui-link-inherit":"cl ui-link-inherit";
			temp.find("a:eq(0)").attr("class", senderType);
			temp.find("span[class='state']").html(data.messageOut.senderType === "P"?"회원":"동호회");
			temp.find("span[class='tit']").html(data.messageOut.senderUserInfo.nickname);
			temp.find("span[class='sub']").html(data.messageBody);
			temp.find("time").html(util.fTime(data.messageOut.sendTime, ":"));
			
			if( data.isRead != "N" ) notReadCount ++;
		}

		$(".count_01").html(document.globalObj.NOTIFICATION_COUNT_DATA.booking);
		$(".count_02").html(document.globalObj.NOTIFICATION_COUNT_DATA.notification);
		$(".count_03").html(document.globalObj.NOTIFICATION_COUNT_DATA.letter = notReadCount);
		
		listview.listview("refresh");
		
		var addBtn = $("#letter_add_btn");
		addBtn.show();
		
		if(listview.find("li").length >= $totalCount) {
			addBtn.hide();
		}
		
		listview.find("a").unbind("click");
		listview.find("a").bind("click", function($event){
			console.log(thisIncLetter.TAG + "아이템 클릭!!!");
			var id = $(this).attr("id");
			var data = thisIncLetter.letterTotalList[id];
			
			console.log("시퀀스번호In:" + data.messageInSeq);
			
			document.globalObj.LETTER_DATA.q_seq = data.messageInSeq;
			document.globalObj.LETTER_DATA.q_type = "IN";
			document.globalObj.LETTER_DATA.q_ynSaved = "N";

			$.mobile.changePage("letter_view.html");
			
			/*
	        this.LETTER_DATA = {//쪽지 view 에 필요 한 데이터
	            q_seq : "80"//해당 시컨스
	            ,
	            q_type : 'OUT'//쪽지 타입 수신 :  IN,    발신  : OUT
	            ,
	            q_ynSaved : 'Y' // 보관여부 : Y or N
	        };
		 */
			
		});
		
		addBtn.unbind("click");
		addBtn.on("click", function($event){
			console.log(thisIncLetter.TAG + "더보기!!");
			thisIncLetter.pageControl.addView();
			thisIncLetter.getMyRecMessageList();
		});
		
		var tSelector = $("#t_checkbox");
		tSelector.unbind("change");
		tSelector.bind( "change", function(event, ui) {
			//console.log(thisIncLetter.TAG + "전체선택:"+$(this).is(":checked"));
			var isChecked = $(this).is(":checked");

			listview.find("input[type='checkbox']").attr("checked", isChecked );
			listview.find("input[type='checkbox']").checkboxradio("refresh");
			
			isChecked =! isChecked;
		});
		
		var saveBtn = $("#letter_save_btn");
		saveBtn.unbind("click");
		saveBtn.on("click",function($event){
			console.log(thisIncLetter.TAG + "보관버튼!");
			thisIncLetter.saveMessage();
		});
		
		var delBtn = $("#letter_delete_btn");
		delBtn.unbind("click");
		delBtn.on("click", function($event){
			console.log(thisIncLetter.TAG + "삭제버튼!");
			thisIncLetter.deleteMessage();

		});
		console.log(thisIncLetter.TAG + "End of viewList()");
	},
	saveMessage: function() 
	{
		console.log(this.TAG + "saveMessage()");
		
		var listview = this.HTMLDom.listview;
		var boxList = listview.find("input[type=checkbox]:checked");
		var messageInSeqList = new Array();
		
		if(boxList.length > 0) {
			for(var i=0; i<boxList.length; i++) 
			{
				var position = boxList.eq(i).attr("id").split("_")[1];
				messageInSeqList.push(this.letterTotalList[position].messageInSeq);
			}
	
			var mJson = new MJson();
			mJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
			mJson.putPub(MJson.PUB.SID, "MG001");
			mJson.putPub(MJson.PUB.SERVICENAME, "saveMessage");
			mJson.putParameter(MJson.PARAMETER.MESSAGEINSEQ, messageInSeqList.join(","));
			//alert(mJson.getJsonString());
	
			var cm = new ConnectionManager();
			cm.requestGet(mJson.getJsonString(), function(jsonData){
	
				if(mJson.checkServiceResult(jsonData)) 
				{
					console.log("성공!!!");
					
					thisIncLetter.letterTotalList = new Array();
					
					thisIncLetter.pageControl.clear();
					
					thisIncLetter.getMyRecMessageList();
				}
				
			}, function(e){
				alert("error:"+JSON.stringify(e));
			});
		}
		else {
			alert("선택하지 않았거나, 리스트가 없습니다.");
		}
	},
	deleteMessage: function() 
	{
		console.log(this.TAG + "deleteMessage()");
		
		var listview = this.HTMLDom.listview;
		var boxList = listview.find("input[type=checkbox]:checked");
		var messageInSeqList = new Array();

		if(boxList.length > 0) 
		{
			for(var i=0; i<boxList.length; i++) 
			{
				var position = boxList.eq(i).attr("id").split("_")[1];
				messageInSeqList.push(this.letterTotalList[position].messageInSeq);
			}
	
			var mJson = new MJson();
			mJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
			mJson.putPub(MJson.PUB.SID, "MG001");
			mJson.putPub(MJson.PUB.SERVICENAME, "deleteMessage");
			mJson.putParameter(MJson.PARAMETER.MESSAGEINSEQ, messageInSeqList.join(","));
			mJson.putParameter(MJson.PARAMETER.INOUTFLAG, "IN");
			//alert(mJson.getJsonString());
			var cm = new ConnectionManager();
			cm.requestGet(mJson.getJsonString(), function(jsonData){
				
				if(mJson.checkServiceResult(jsonData)) 
				{
					console.log("성공!!!");
					
					thisIncLetter.letterTotalList = new Array();
					thisIncLetter.pageControl.clear();
					
					thisIncLetter.getMyRecMessageList();
				}
				
			}, function(e){
				alert("error:"+JSON.stringify(e));
			});
		}
		else {
			alert("선택하지 않았거나, 리스트가 없습니다.");
		}
	}
}






/*function Message($serviceName, $parameterName, $parameter, $context) {
	thisMessage = this;
	this.TAG = " < Message > ";
	this.serviceName = $serviceName;
	this.parameterName = $parameterName;
	this.parameterName = $parameter;
	this.context = $context;
}

Message.prototype = {
		req: function() {
			var mJson = new MJson();
			mJson.putClient(MJson.CLIENT.USERID, document.globalObj.LOGIN_DATA.userId);
			mJson.putPub(MJson.PUB.SID, "MG001");
			mJson.putPub(MJson.PUB.SERVICENAME, this.serviceName);
			mJson.putParameter(this.parameter, this.parameter);
			
			var cm = new ConnectionManager();
			cm.requestGet(mJson.getJsonString(), function(jsonData){
				thisMessage.context.getMyRecMessageList();
			}, function(e){
				alert("error:"+JSON.stringify(e));
			})
		}
}

function SaveMessage() {
	Message.apply(this, arguments);
}

SaveMessage.prototype = new Message();

SaveMessage.prototype.constructor = SaveMessage;

function DeleteMessage() {
	Message.apply(this, arguments);
}

DeleteMessage.prototype = new Message();

DeleteMessage.prototype.constructor = DeleteMessage;

DeleteMessage.prototype = {
		vv: function() {
			
		}
}
*/


